package com.zhoug.map3;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;

import com.zhoug.common3.Logger;

import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapView;
import org.osmdroid.views.Projection;
import org.osmdroid.views.overlay.Polygon;

import java.util.ArrayList;
import java.util.List;

/**
 * 行政区域边界绘制,遮罩边界之外的区域
 * @Author: zhoug
 * @Date: 2025-01-06
 * @Description:
 */
public class PolygonMask extends Polygon {
    private static final String TAG = ">>>PolygonMask";
    private static final boolean DEBUG = false;
    private int maskColor = 0xFFFFFFFF;//遮罩颜色
    private List<PolygonHole> mPolygonBounds = new ArrayList<>();
    private MapView mMapView;
    private boolean mDrawMask=true;

    public PolygonMask(MapView mapView) {
        this.mMapView = mapView;
        Paint outlinePaint = getOutlinePaint();
        outlinePaint.setColor(0xFFFFFFFF);
        outlinePaint.setStrokeWidth(Utils.dpTopx(mapView.getContext(),2));
    }

    public void setDrawMask(boolean drawMask) {
        this.mDrawMask = drawMask;
    }

    public void setMaskColor(int maskColor) {
        this.maskColor = maskColor;
    }

    @Override
    public void draw(final Canvas pCanvas, final Projection pProjection) {
        //保障状态并创建新图层,避免把绘制的地图清除了
        int id = pCanvas.saveLayer(0, 0, pCanvas.getWidth(), pCanvas.getHeight(), null);
        if (DEBUG) {
            Logger.d(TAG, "draw:" + id);
        }
        //绘制遮罩
        if(mDrawMask){
            pCanvas.drawColor(maskColor);
        }
        if (mPolygonBounds != null && !mPolygonBounds.isEmpty()) {
            for (PolygonHole polygonHole : mPolygonBounds) {
                polygonHole.draw(pCanvas, pProjection);
            }
        }
        pCanvas.restoreToCount(id);
    }

    @Deprecated
    @Override
    public void setPoints(List<GeoPoint> points) {
        super.setPoints(points);
    }

    @Override
    public void setHoles(List<? extends List<GeoPoint>> holes) {
        if (holes != null && !holes.isEmpty()) {
            for (List<GeoPoint> item : holes) {
                if (item != null && !item.isEmpty()) {
                    PolygonHole polygon = new PolygonHole(mMapView);
                    polygon.setInfoWindow(null);
                    // 设置线颜色
                    Paint outlinePaint = polygon.getOutlinePaint();
                    outlinePaint.setColor(getOutlinePaint().getColor());
                    // 设置线宽度
                    outlinePaint.setStrokeWidth(getOutlinePaint().getStrokeWidth());
                    Paint fillPaint = polygon.getFillPaint();
                    fillPaint.setColor(0x55000000);
                    polygon.setPoints(item);
                    mPolygonBounds.add(polygon);
                }
            }
        }
    }

    public List<PolygonHole> getPolygonBounds() {
        return mPolygonBounds;
    }

    public void remove() {
        if(mPolygonBounds!=null && !mPolygonBounds.isEmpty()){
            for (PolygonHole polygonHole : mPolygonBounds) {
                if(polygonHole!=null){
                   mMapView.getOverlays().remove(polygonHole);
                }
            }
        }
        mMapView.getOverlays().remove(this);
    }


    @Override
    public void onDestroy() {
        if(mPolygonBounds!=null && !mPolygonBounds.isEmpty()){
            for (PolygonHole polygonHole : mPolygonBounds) {
                if(polygonHole!=null){
                    polygonHole.onDestroy();
                }
            }
        }
        super.onDestroy();
    }

    @Override
    public void onDetach(MapView mapView) {
        if(mPolygonBounds!=null && !mPolygonBounds.isEmpty()){
            for (PolygonHole polygonHole : mPolygonBounds) {
                if(polygonHole!=null){
                    polygonHole.onDetach(mapView);
                }
            }
        }
        super.onDetach(mapView);
    }

    /**
     * 行政区域每个边界区域
     */
    public static class PolygonHole extends Polygon {
        private static final String TAG = ">>>PolygonHole";

        public PolygonHole() {
        }

        public PolygonHole(MapView mapView) {
            super(mapView);
        }


        @Override
        public void draw(final Canvas pCanvas, final Projection pProjection) {
            mFillPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
            super.draw(pCanvas, pProjection);
        }


        @Deprecated
        @Override
        public void setHoles(List<? extends List<GeoPoint>> holes) {
//        super.setHoles(holes);
        }
    }


}
